package com.beta.admin.controller;

import com.beta.admin.support.web.BaseController;
import com.beta.common.web.ContentType;
import com.beta.service.ICitiesService;
import com.beta.service.IProvincesService;
import com.beta.vo.CityVO;
import com.beta.entity.Cities;
import ma.glasnost.orika.MapperFacade;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

/**
 * Created by lichao on 2017/1/6.
 */
@RestController
@RequestMapping(value = RequestMappingConstants.MAPPING_PREFIX + "/cities")
public class CitiesController extends BaseController {

    @Autowired
    private IProvincesService provincesService;

    @Autowired
    private ICitiesService citiesService;

    @Autowired
    private MapperFacade beanMapper;

    @GetMapping(value = "", produces = {ContentType.JSON})
    public Object getCities(@RequestParam(value = "key", defaultValue = "") String key) {
//        return ok(provincesService.getCities());
        if (StringUtils.isBlank(key)) {
            return ok(Collections.emptyList());
        }

        List<Cities> cities = citiesService.getCitiesByKey(key);
        return ok(beanMapper.mapAsList(cities, CityVO.class));
    }

}
